<template>
  <div>
    <swiper
      :options="swiperOption"
      ref="mySwiper"
      class="my-swiper"
      v-if="bannerList.length > 1"
    >
      <swiper-slide
        class="banner-item"
        v-for="item in bannerList"
        :key="item.id"
      >
        <img :src="item.url" alt="图片" />
      </swiper-slide>
      <div class="swiper-pagination" slot="pagination"></div>
      <div class="swiper-button-prev" slot="button-prev"></div>
      <div class="swiper-button-next" slot="button-next"></div>
    </swiper>
  </div>
</template>

<script>
import bannerApi from "@/api/banner";
export default {
  name: "first",
  data() {
    return {
      swiperOption: {
        loop: true,
        autoplay: {
          delay: 2500,
          disableOnInteraction: false,
        },
        pagination: {
          el: ".swiper-pagination",
          clickable: true,
        },
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev",
        },
      },
      bannerList: [],
    };
  },
  computed: {
    swiper() {
      return this.$refs.mySwiper.swiper;
    },
  },
  mounted() {
    this.swiper.slideTo(0, 1000, true);
  },
  created() {
    this.getBannerLists();
  },
  methods: {
    getBannerLists() {
      bannerApi
        .getBannerList()
        .then(({ data: res }) => {
          if (res.code === 200) {
            this.bannerList = res.data.data;
          } else {
            return false;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.my-swiper {
  width: 100%;
  height: 600px;
  background: red;
}
.banner-item {
  width: 100%;
  height: 100%;
  background: #f5f;
  img {
    width: 100%;
    height: 100%;
  }
}
</style>